//
//  ViewController.m
//  UI12_UITableView自定义Cell
//
//  Created by LGC on 16/3/24.
//  Copyright © 2016年 Apon. All rights reserved.
//

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#import "ViewController.h"
#import "TableViewOfNews.h"
#import "ModelOfNews.h"
#import "JSONParser.h"
#import "CellOfNews.h"

static NSString *pool = @"pool";

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)TableViewOfNews *tableView;

@property (nonatomic, retain) NSMutableArray *arrOfModel;

@end

@implementation ViewController
#pragma mark - self.
- (void)dealloc {
    [_tableView release];
    [_arrOfModel release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)handleData {
    id result = [JSONParser JSONParserWithFileName:@"news" type:@"txt"];
    
    NSArray *arrOfNews = [result objectForKey:@"news"];
    
    self.arrOfModel = [NSMutableArray array];
    // 遍历数组.
    for (NSDictionary *smallDic in arrOfNews) {
        
        ModelOfNews *model = [[ModelOfNews alloc]init];
        
        [model setValuesForKeysWithDictionary:smallDic];
        
        [self.arrOfModel addObject:model];
    }
    

}


#pragma mark - tableView
- (void)createTableView {
    
    self.tableView = [[TableViewOfNews alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
    #pragma mark - 自定义cell
    /** 详见CellOfNewS类. */
    
    // 注册.
    [self.tableView registerClass:[CellOfNews class] forCellReuseIdentifier:@"pool"];

}



#pragma mark - dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrOfModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfNews *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelOfNews *model = [[self.arrOfModel objectAtIndex:indexPath.row]retain];
    
    [cell setCellContentValues:model];
    
    [model release];
    
    return cell;
}

#pragma mark - Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelOfNews *model = [self.arrOfModel objectAtIndex:indexPath.row];
    
    return [CellOfNews heightOfCellNews:model.summary];
    
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
